<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="所属院系">
              <a-select allowClear v-model="queryParam.deptCode" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="教室类型">
              <a-select allowClear v-model="queryParam.typeCode">
                <a-select-option v-for="i in TYPE0046" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select> 
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item label="教室名称">
                <a-input v-model="queryParam.classroomNo"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="开课学年">
                <a-select allowClear v-model="queryParam.kkxn">
                  <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="开课学期">
                <a-select allowClear v-model="queryParam.kkxqm">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="8" :sm="12">
              <a-form-item label="上课周类型">
                <a-select allowClear v-model="queryParam.skzlxm">
                  <a-select-option v-for="i in TYPE0343" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="上课周">
                <a-select allowClear v-model="queryParam.skz">
                  <a-select-option v-for="i in TYPE0344" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="开始周">
                <a-input v-model.number="queryParam.ksz"/>
              </a-form-item>
            </a-col>
             <a-col :md="8" :sm="12">
              <a-form-item label="结束周">
                <a-input v-model.number="queryParam.jsz"/>
              </a-form-item>
            </a-col>
             <a-col :md="8" :sm="12">
              <a-form-item label="开始课节">
                <a-input v-model.number="queryParam.kskj"/>
              </a-form-item>
            </a-col>
             <a-col :md="8" :sm="12">
              <a-form-item label="结束课节">
                <a-input v-model.number="queryParam.jskj"/>
              </a-form-item>
            </a-col> -->
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="loadData()" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="exportExcel" style="margin:0 10px">导出</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>
        
    <a-card :bordered="false" style="margin-top: 10px" class="weekTable">
      <a-table
        v-if="initDone"
        ref="classTable"
        size="default"
        :columns="weekColumns"
        :data-source="dataOpen"
        :alert="{show: false}"
        :pagination="false"
        :loading="confirmLoading"
        bordered
      >
        <template slot="jczc" slot-scope="a,b,index">
            <p style="border-bottom: 1px solid #e8e8e8;margin-bottom: 0;padding:16px">{{index*2 + 1}}</p>
            <p style="margin-bottom: 0;padding:16px">{{index*2 + 2}}</p>
        </template>
      </a-table>
    </a-card>
    <a-card :bordered="false" style="margin-top: 10px">
      <a-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data-source="data"
        :loading="loading"
        :alert="{show: false}"
        :pagination="pagination"
        :expandRowByClick="true"
        :expandedRowKeys="expandedRowKeys"
        @change="handleTableChange"
        @expand="exChang"
      >
        <div
          slot="expandedRowRender"
          slot-scope="record"
          style="margin: 0">
          <a-row
            v-for="(pkxx, index) in record.pkxxs" :key="index"
            :gutter="8"
            style="word-wrap: break-word;margin-bottom: 2px">
              <a-col :span="3">
                <span>{{ pkxx.kcbm }}</span>
              </a-col>
              <a-col :span="3">
                <span>{{ pkxx.kclbmc }}</span>
              </a-col>
              <a-col :span="3">
                <span>{{ pkxx.kcmc }}</span>
              </a-col>
              <a-col :span="2">
                <span>{{ pkxx.skzlxmc }}</span>
              </a-col>
              <a-col :span="2">
                <span>上课周：{{ pkxx.skzmc }}</span>
              </a-col>
              <a-col :span="2">
                <span>开始周：{{ pkxx.ksz }}</span>
              </a-col>
              <a-col :span="3">
                <span>结束周：{{ pkxx.jsz }}</span>
              </a-col>
              <a-col :span="3">
                <span>开始课节：{{ pkxx.kskj }}</span>
              </a-col>
              <a-col :span="3">
                <span>结束课节：{{ pkxx.jskj }}</span>
              </a-col>
          </a-row>
          <!-- {{ record.gh }} -->
        </div>
      </a-table>
    </a-card>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  data () {
    return {
      dataOpen: [],
      expandedRowKeys: [],
      data:[],
      loading: false,
      advanced: true,
      queryParam: {},
      pagination:{
        current:1,
        pageSize: 10,
        showSizeChanger: true,
        pageSizeOptions: ['10', '20', '30','40'],
        // 改变pageSize后触发
        onShowSizeChange: (current, pageSize) => this.pagination.pageSize = pageSize,
      },
      weekColumns:[
        {
          title: '节次/周次',
          dataIndex: 'jsxqmc',
          scopedSlots: { customRender: 'jczc' },
        },
        {
          title: '星期一',
          dataIndex: 'one',
          width: 320,
          customRender: (text, record, index) => {
            if(text){
              return <p style="padding: 16px;margin:0;">{text}</p>;
            }else{
              return <p style="color:#999;padding: 16px;margin:0;">-</p>;
            }
            
          }

        },
        {
          title: '星期二',
          dataIndex: 'two',
          width: 320,
          customRender: (text, record, index) => {
            if(text){
              return <p style="padding: 16px;margin:0;">{text}</p>;
            }else{
              return <p style="color:#999;padding: 16px;margin:0;">-</p>;
            }
            
          }

        },
        {
          title: '星期三',
          dataIndex: 'three',
          width: 320,
          customRender: (text, record, index) => {
            if(text){
              return <p style="padding: 16px;margin:0;">{text}</p>;
            }else{
              return <p style="color:#999;padding: 16px;margin:0;">-</p>;
            }
            
          }
        },
        {
          title: '星期四',
          dataIndex: 'four',
          width: 320,
          customRender: (text, record, index) => {
            if(text){
              return <p style="padding: 16px;margin:0;">{text}</p>;
            }else{
              return <p style="color:#999;padding: 16px;margin:0;">-</p>;
            }
            
          }
        },
        {
          title: '星期五',
          dataIndex: 'five',
          width: 320,
          customRender: (text, record, index) => {
            if(text){
              return <p style="padding: 16px;margin:0;">{text}</p>;
            }else{
              return <p style="color:#999;padding: 16px;margin:0;">-</p>;
            }
            
          }
        },
        {
          title: '星期六',
          dataIndex: 'sixe',
          width: 320,
          customRender: (text, record, index) => {
            if(text){
              return <p style="padding: 16px;margin:0;">{text}</p>;
            }else{
              return <p style="color:#999;padding: 16px;margin:0;">-</p>;
            }
            
          }
        },
        {
          title: '星期日',
          dataIndex: 'seve',
          width: 320,
          customRender: (text, record, index) => {
            if(text){
              return <p style="padding: 16px;margin:0;">{text}</p>;
            }else{
              return <p style="color:#999;padding: 16px;margin:0;">-</p>;
            }
            
          }
        },
      ],
      columns: [
        {
          title: '校区',
          dataIndex: 'jsxqmc'
        },
        {
          title: '教室类型',
          dataIndex: 'typeCodeName'
        },
        {
          title: '教室名称',
          dataIndex: 'classroomNo'
        },
        {
          title: '位置',
          dataIndex: 'location'
        },
        {
          title: '容量',
          dataIndex: 'capacity'
        }
      ],
      // loadData: parameter => {
      //   parameter.pageNum = parameter.pageNo
      //   let params = Object.assign(this.queryParam, parameter)
      //   return this.$api.cultivate.classroomPkList(params).then(res => {
      //     return {
      //       data: res.data.rows,
      //       pageNo: parameter.pageNo,
      //       pageSize: parameter.pageSize,
      //       totalPage: Math.ceil(res.data.total / parameter.pageSize),
      //       totalCount: res.data.total
      //     }
      //   })
      // },
      selectMenu: {},
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'],
      TYPE0046: state => state.app['TYPE0046'],
      TYPE0343: state => state.app['TYPE0343'],
      TYPE0344: state => state.app['TYPE0344'],
      TYPE0072: state => state.app['TYPE0072']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0046')
    this.$store.dispatch('app/setDictionary', 'TYPE0343')
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0344')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.$set(this.queryParam, 'kkxqm', res[1].currentSemester)
        this.$set(this.queryParam, 'kkxn', res[1].schoolYear)
        this.initDone = true
        this.loadData({
          pageNum:1,
          pageSize:10
        })
      })
    },
    loadData(parameter = {}) {
      if(!parameter.pageNum){
         this.pagination.current = 1
         parameter.pageNum = 1
       }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.confirmLoading = true
      console.log(this.pagination.pageSize)
      this.$api.cultivate.classroomPkList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data && res.data.rows.length) {
          this.data = res.data.rows;
        }
        this.loading = false;
        this.confirmLoading = false
        console.log(this.data[0].id)
        this.exChang(true,this.data[0])
      })
    },
    // 分页
			handleTableChange(pagination, filters, sorter) {
				console.log(pagination);
				const pager = { ...this.pagination
				};
				pager.current = pagination.current;
				this.pagination = pager;
				this.loadData({
					pageSize: this.pagination.pageSize,
					pageNum: pagination.current
				});
			},
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    exportExcel () {
      let params = {...this.queryParam}
      delete params.pageNum
      this.$export('/foster/pkxx/classroomPkqkExp', params)
    },
    exChang(expanded,record){
      this.expandedRowKeys=[record.id.toString()]
      this.confirmLoading = true
      console.log(expanded,record)
      if(record.cousePlan){
        this.dataOpen = record.cousePlan.map((item,index) => {
          return {one:item[0],two:item[1],three:item[2],four:item[3],five:item[4],sixe:item[5],seve:item[6]}
        })
        console.log(this.dataOpen)
      }
      setTimeout(()=> {
        this.confirmLoading = false
      },500)
      
      
    },
  }
}
</script>

<style lang="less" scoped="">
.weekTable /deep/ .ant-table-tbody > tr > td{
  padding: 16px 0;
  // text-align: center;
}

</style>
